(function($){
    $.fn.thumbNav=function(settings){
      var defaults={
        thumbPerPage:           4,
        maxPageIndice:          3,
        navContainer:           '#thumbNav',
        thumbContainer:         '.galleryThumbDiv',
        lockHeight:             true
      };
      
      $.extend(this,{
        lockContainerHeight: function(){
            if(this.lockHeight) this.height(this.height());
            return this;
        },
        buildPager: function(){
            if(this.pageNum<2) return this;
            if(!this.$pager) this.$pager=this.prepend('<div id="pagerContainer"><div id="pager"></div></div>').find('#pager');
            this.$pager.empty();
            //calculate page indice range
            var startPage=1;
            var endPage=this.pageNum;
            var p=this.currentPage;
            var m=this.maxPageIndice;
            var t=this.pageNum;
            var a=p-Math.ceil(m/2);
            var b=t-Math.ceil(m/2)-p+1;
            if(a<1 && b>0) endPage=m;
            else if(b<1 && a>0) startPage=endPage-m+1;
            else if(a>0 && b>0) {startPage=p-Math.floor((m-1)/2);endPage=p+Math.floor(m/2);}
            //previous page
            if(p>1) this.$pager.append('<div rel="'+(parseInt(p)-1)+'">&lt;Prev</div>').find('div:last').click(function(){thumbNav.showPage($(this).attr('rel'));});
            //first page
            if(startPage>1) this.$pager.append('<div>1</div>...').find('div:last').click(function(){thumbNav.showPage(1);});
            //pages
            for(i=startPage;i<endPage+1;i++){
                if(i==p) this.$pager.append('<div class="currentPage">'+i+'</div>');
                else this.$pager.append('<div rel="'+i+'">'+i+'</div>').find('div:last').click(function(){thumbNav.showPage($(this).attr('rel'));});
            }
            //last page
            if(endPage<t) this.$pager.append('...<div rel="'+t+'">'+t+'</div>').find('div:last').click(function(){thumbNav.showPage($(this).attr('rel'));});
            //next page
            if(p<t) this.$pager.append('<div rel="'+(parseInt(p)+1)+'">Next&gt;</div>').find('div:last').click(function(){thumbNav.showPage($(this).attr('rel'));});
            return this;
        },
        buildPage: function(pageIndex){
            this.currentPage=pageIndex;
            var startThumb=(this.currentPage-1)*this.thumbPerPage+1;
            var endThumb=Math.min(this.currentPage*this.thumbPerPage, this.thumbNum);
            var i=1;
            this.find('div'+this.thumbContainer).each(function(){
                if(i>=startThumb && i<=endThumb) $(this).show();
                else $(this).hide();
                ++i;
            });
            this.buildPager();
            this.fadeTo(300,1.0);
            return this;
        },
        showPage: function(pageIndex){
            this.fadeTo(300,0.0,function(){thumbNav.buildPage(pageIndex);});
            return this;
        }
      });
      $.extend(this, defaults, settings);
      var thumbNav=this;
      this.currentPage=1;
      this.thumbNum=this.find('div'+this.thumbContainer).length;
      this.pageNum=Math.ceil(this.thumbNum/this.thumbPerPage);
      this.hide();
      this.buildPage(1);
      this.lockContainerHeight();
      return this;  
    };
})(jQuery);